package us.mmapp.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.format.DateFormat;
import android.widget.TextView;

public class TextUtils {
	private TextUtils(){}
	/**
	 * Converting stream to string.
	 * 
	 * @param is
	 * @return
	 */
	public static String convertStreamToString(InputStream is)
	{
		if (is != null)
		{
			Writer writer = new StringWriter();
			
			char[] buffer = new char[1024];
			
			try 
			{	
				Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				int n;
				while ((n = reader.read(buffer)) != -1)
				{
					writer.write(buffer, 0, n);
				}
			}
			catch (Exception ex)
			{}
			return writer.toString();
		}
		return "";
	}
	/**
	 * Get MD5 Hash.
	 * @param in
	 * @return md5 hash 
	 */
	public static String md5(String in)
	{
		MessageDigest digest;
		String ret = null;
		try
		{
			digest = MessageDigest.getInstance("MD5");
			digest.reset();
			digest.update(in.getBytes());
			BigInteger i = new BigInteger(1,digest.digest());
			return String.format("%1$032X", i);
		}
		catch (NoSuchAlgorithmException e)
		{}
		return ret;
	}
	public static String convertDate(long dateCreated)
	{
		 java.text.DateFormat[] formats = new java.text.DateFormat[] {
				   SimpleDateFormat.getDateInstance(),
				   SimpleDateFormat.getDateTimeInstance(),
				   SimpleDateFormat.getTimeInstance(),
				 };

		 
		return formats[0].format(new Date(dateCreated * 1000)) + "\n" +formats[2].format(new Date(dateCreated * 1000));
	}
   
}
